package com.rchat.pocmini.activity.forgetpwd;

import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.view.ClearEditText;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/7/28.
 */

public class ForgetPwdMvpActivity extends BaseMvpActivity<ForgetPwdPresenterImpl, ForgetPwdModelImpl> implements ForgetPwdContract.View {

    @BindView(R.id.et_phone)
    ClearEditText mEtPhone;

    @BindView(R.id.et_identify_code)
    ClearEditText mEtIdentifyCode;

    @BindView(R.id.et_pwd)
    ClearEditText mEtPwd;

    @BindView(R.id.et_pwd_confirm)
    ClearEditText mEtPwdConfirm;

    @BindView(R.id.btn_identify_code_timer)
    Button mBtnIdentifyCodeTimer;


    TimeCount mTimeCount;

    @BindView(R.id.pb)
    ProgressBar mPb;


    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.forget_password_activity_layout;
    }

    @Override
    public void initView() {
        mTimeCount = new TimeCount(60000, 1000);

        mEtPhone.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 11) {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_normal));
                } else {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_unnormal));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (temp.length() == 11) {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_normal));
                    mBtnIdentifyCodeTimer.setClickable(true);
                } else {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_unnormal));
                    mBtnIdentifyCodeTimer.setClickable(false);
                }
            }
        });
    }

    @Override
    public void doAfterInitView() {

    }

    @Override
    protected void onDestroy() {
        if (mTimeCount != null) {
            mTimeCount.cancel();
        }
        super.onDestroy();
    }

    @Override
    public void getVerificationCodeBegin() {
        mTimeCount.start();
    }

    @Override
    public void setProcessBarVisibility(int visibility) {
        mPb.setVisibility(visibility);
    }

    @Override
    public void finishSelf() {
        finish();
    }

    @OnClick({R.id.btn_identify_code_timer,
            R.id.iv_commit,
            R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_identify_code_timer:
                mPresenter.getVerificationCode(
                        mEtPwd.getText().toString().trim(),
                        mEtPwdConfirm.getText().toString().trim(),
                        mEtPhone.getText().toString().trim());
                break;
            case R.id.iv_commit:
                mPresenter.resetPwd(
                        mEtIdentifyCode.getText().toString().trim(),
                        mEtPhone.getText().toString().trim(),
                        mEtPwd.getText().toString().trim(),
                        mEtPwdConfirm.getText().toString().trim());
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }


    /**
     * 获取验证码60秒计时器
     */
    private class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            if (mBtnIdentifyCodeTimer != null) {
                mBtnIdentifyCodeTimer.setText(R.string.register_get_verification_code);
                mBtnIdentifyCodeTimer.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            if (mBtnIdentifyCodeTimer != null) {
                mBtnIdentifyCodeTimer.setClickable(false);//防止重复点击
                mBtnIdentifyCodeTimer.setText(millisUntilFinished / 1000 + getString(R.string.second));
            }
        }
    }
}
